package P150;

/**
 * @author LiangMu
 * @version \$Id: P20.java, v 0.1, 2024年07月04日 19:48 LiangMu Exp $$
 */
public class P20 {

    public static String reverseWords(String s) {
        String s1 = s.replaceAll(" +", " ").trim();
        String[] strs = s1.split(" ");
        StringBuilder result = new StringBuilder();
        for (int i = strs.length - 1; i >= 0; i--) {
            result.append(strs[i]);
            if (i > 0) {
                result.append(" ");
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String s = "  hello world  ";
        System.out.println(reverseWords(s));
    }

}
